namespace :acr do
  desc "Video auto copy"
  task :avi_copy => :environment do
    require 'csv'
    pa = "#{BILI_PRO_PATH}#{ENV['pa']}\\"
    csv_file = File.open "#{pa}_list.csv"
    csv_data = CSV::IOReader.new(csv_file)
    list_count = (ENV['list_count'] || 30).to_i
    data = []
    csv_data.each do |row|
      data << { :work_id => row[2], :total_rank => row[0] } unless row[0].to_i.zero?
      break if row[0].to_i > list_count + 1
    end
    data.sort_by{|d| d[:total_rank].to_i}[0...list_count].each_with_index do |d, i|
      if File.exist? %[#{pa}..\\video\\av#{d[:work_id]}_.avi]
         system %[move /Y #{pa}..\\video\\av#{d[:work_id]}_.avi #{pa}2_list1\\top#{i+1}_.avi]
      end
       if File.exist? %[#{pa}..\\video\\av#{d[:work_id]}.avi]
         system %[move /Y #{pa}..\\video\\av#{d[:work_id]}.avi #{pa}2_list1\\top#{i+1}.avi]
       else
         unless File.exist? %[#{pa}2_list1\\top#{i+1}.avi]
           system %[copy #{pa}..\\video\\default.avi #{pa}2_list1\\top#{i+1}.avi]
         end
       end
    end
  end
end


